/**
不同地图坐标系坐标系转换类
*/
(function(){        //闭包
	function load_script(xyUrl, callback){
		var head = document.getElementsByTagName('head')[0];
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = xyUrl;
		//借鉴了jQuery的script跨域方法
		script.onload = script.onreadystatechange = function(){
			if((!this.readyState || this.readyState === "loaded" || this.readyState === "complete")){
				callback && callback();
				// Handle memory leak in IE
				script.onload = script.onreadystatechange = null;
				if ( head && script.parentNode ) {
					head.removeChild( script );
				}
			}
		};
		// Use insertBefore instead of appendChild  to circumvent an IE6 bug.
		head.insertBefore( script, head.firstChild );
	}
	function translate(point,type,callback){
		var callbackName = 'cbk_' + Math.round(Math.random() * 10000);    //随机函数名
		var xyUrl = "http://api.map.baidu.com/ag/coord/convert?from="+ type + "&to=4&x=" + point.lng + "&y=" + point.lat + "&callback=BMap.Convertor." + callbackName;
		//动态创建script标签
		load_script(xyUrl);
		BMap.Convertor[callbackName] = function(xyResult){
			delete BMap.Convertor[callbackName];    //调用完需要删除改函数
			var point = new BMap.Point(xyResult.x, xyResult.y);
			callback && callback(point);
		}
	}
	
	window.BMap = window.BMap || {};
	BMap.Convertor = {};
	BMap.Convertor.translate = translate;
})();

var bmap={
	initialize:function(point,city) {
		//bmap.initializeIcons();
		bmap.map = new BMap.Map('mapContainer');
		if(!point) point = new BMap.Point(120.121636, 30.281320);
		bmap.map.centerAndZoom(point, 13); 
		bmap.map.addControl(new BMap.NavigationControl());
		bmap.map.addControl(new BMap.ScaleControl());
		bmap.map.addControl(new BMap.OverviewMapControl());
		bmap.map.addControl(new BMap.MapTypeControl());
		if(!city) city="杭州";
		bmap.map.setCurrentCity(city);
	},
	addMark:function(lat,lng, title,ctx){
		if(!bmap.markerClusterer){
			bmap.markerClusterer = new BMapLib.MarkerClusterer(bmap.map);
			bmap.markerClusterer.setGridSize(50);
			bmap.markerClusterer.setMaxZoom(14);
		}
		var p = new BMap.Point(lng, lat);
		var mrk = new BMap.Marker(p); // 创建标注 
		mrk.setTitle(title);
		var win = bmap.makeInfoWin(title,p,ctx);
		mrk.infoWin = win;
		mrk.addEventListener("click", function(e){
			bmap.map.openInfoWindow(win, e.point);
		});
		//bmap.markerClusterer.addMarker(mrk);
		bmap.map.addOverlay(mrk);
		return mrk;
	},
	removeMark:function(marker){
		//bmap.markerClusterer.removeMarker(marker);
		bmap.map.removeOverlay(marker);
		marker.removeEventListener("click");
		marker.dispose();
	},
	moveTo:function(lat,lng){
		var p = new BMap.Point(lng, lat);
		bmap.map.panTo(p);
	},
	
	makeInfoWin:function(title,p,ctx){
		var opts = {
			title:"<b>"+title+"</b>",
			width : 400, // 信息窗口宽度 
			height: 150//, // 信息窗口高度
		}
		return new BMap.InfoWindow(ctx,opts); // 创建信息窗口对象 
	},
	loadData:function (url, onSucc){
		jQuery.ajax({
			type : 'GET',
			contentType : 'text/html',
			url : url,
			success : function(data) {
				if(onSucc){
					onSucc(data);
				}
			},
			error : function(data) {
					//alert(data)
			}
		});
	},
	/*通过百度PlaceAPI获取后端JSON数据*/
	loadPlaceAPI:function(name, pos,city,callback){
	    var url = "";
	    if(pos){
	        url = 'http://api.map.baidu.com/place/search?&query='+encodeURI(name)+'&location='+pos.lat+','+pos.lng+'&radius=1000&output=json&key=ddc24df86291884d37a8f95336b9f9ca';
	    }else{
	        url = 'http://api.map.baidu.com/place/search?&query='+encodeURI(name)+'&region='+encodeURI(city)+'&output=json&key=ddc24df86291884d37a8f95336b9f9ca';
	    }
	    bmap.loadData(url,callback);
	}

}